<?php

require_once dirname(__FILE__).'/../lib/priceGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/priceGeneratorHelper.class.php';

include_once 'reader.php';


/**
 * price actions.
 *
 * @package    estate
 * @subpackage price
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class priceActions extends autoPriceActions
{

	public function executeProcessXLS(sfWebRequest $request){
		if( is_array($_FILES) and count($_FILES) > 0 ){
		  $file = $_FILES['file'];
		  if($file['type'] == "application/vnd.ms-excel"){
		    $fname = sfConfig::get('sf_upload_dir')."/".$file['name'];
		    if( move_uploaded_file( $file['tmp_name'], $fname ) ){
		        $this->data = new Spreadsheet_Excel_Reader();
        		// Set output Encoding.
        		$this->data->setOutputEncoding('CP1252');
        		$this->data->read($fname);
        
        		ApartmanPricePeer::doDeleteAll();
        		
        		$rows = $this->data->sheets[0]['numRows'];
        		$cols = $this->data->sheets[0]['numCols'];
        		//$this->data->sheets[0]['cells'][$i][$j];
        		$price = $head = $ap_id = $from = $to = $minp = $maxp = 1;
        		//$head - jelöli h melyik sorban van az aktuális header
        		$this->count = 0;
        		$ti = 0; //temporalis sorvaltozo, tablazatonkent nullazzuk
        		for( $i = 1; $i <= $rows; $i++)
        		{
        		    for( $j = 1; $j <= $cols; $j++ ){
        		      switch ($j){
        					case 1:
        					  
        					  if( isset($this->data->sheets[0]['cells'][$i][$j]) ){
        							if( is_numeric($this->data->sheets[0]['cells'][$i][$j]) ){
        								$ti = 0; //temporalis sorvaltozo, tablazatonkent nullazzuk
        								//ha uj lakas id, akkor beallitjuk a head-et es megtesszuk a lakas_id-t
        								$head = $i;
        								$ap_id = $this->data->sheets[0]['cells'][$i][1];
        								//uj tablat veszunk fel es hozzarendeljuk a lakashoz
        								$t = new jwCustomTable();
        								$t->setDescription("{$ap_id} lakas artablazata");
        								$t->save();
        								$a = ApartmanPeer::retrieveByPK($ap_id);
        								$t_id = $t->getId();
        							    if( $a ){
        								    $a->setPricesTableId($t_id);
        								    $tv = new jwCustomTableValue();
        								    $tv->setJwTableId($t_id);
        								    $tv->setCol(0);
        								    $tv->setRow(0);
        								    $tv->setValue("");
        								    $tv->save();
        								}
        								break;
        							}else{
        								//min_p es max_p belovese
        								$txt = str_replace('fo','',$this->data->sheets[0]['cells'][$i][$j]);
        								$ps = explode('-',$txt);
        								$minp = trim($ps[0]);
        								$maxp = trim($ps[0]);
        								if(count($ps)>1){
        									$maxp = trim($ps[1]);
        								}
        								$tv = new jwCustomTableValue();
        								$tv->setJwTableId($t_id);
        								$tv->setCol(($j-1));
        								$tv->setRow($ti);
        								$tv->setValue($txt);
        								$tv->setParseAs("persons");
        								$tv->save();
        							}
        						}
        						break;
        					default:
        						if( isset($this->data->sheets[0]['cells'][$i][$j]) ){
        							//ha szam, akkor az lakas ar
        							$tv = new jwCustomTableValue();
        						    $tv->setJwTableId($t_id);
        							$tv->setCol($j-1);
        							$tv->setRow($ti);
        							if( !is_numeric( $this->data->sheets[0]['cells'][$i][$j] ) ){
        							  $tv->setParseAs("datetime");
        							}
        							$tv->setValue($this->data->sheets[0]['cells'][$i][$j]);
        							$tv->save();
        						    
        							if( is_numeric( $this->data->sheets[0]['cells'][$i][$j] ) ){								
        							    $price = $this->data->sheets[0]['cells'][$i][$j];
        								//beallitjuk a datumot
        								if( !empty($this->data->sheets[0]['cells'][$head][$j]) ){
        							        $ds = explode('-',$this->data->sheets[0]['cells'][$head][$j]);
            								$from = "2010.".trim($ds[0])." 01:00:00";
            								$from = str_replace('.','-',$from);
            								$to = "2010.".trim($ds[0])." 01:00:00";
            								$to = str_replace('.','-',$to);
            								if( count($ds) > 1 ){
            									$to = "2010.".trim($ds[1])." 01:00:00";
            									$to = str_replace('.','-',$to);
            								}								  
        								}else{
        								  $from = '';
        								  $to = '';
        								}
        								/* lerakjuk az adatokat es elmentjuk */
        								$a = ApartmanPeer::retrieveByPK($ap_id);
        								if( $a ){
        								    $ap = new ApartmanPrice();
        								    $ap->setApartman($a);
            								$ap->setPrice($price);
            								$ap->setDateFrom($from);
            								$ap->setDateTo($to);
            								$ap->setMaxP($maxp);
            								$ap->setMinP($minp);
            								$ap->save();
            								++$this->count;
        								}
        							}
        						}
        						break;
        				}
        			}
        			++$ti;
        		}
		    }
		  }
		}
	}
    
	
	public function executeShowUlForm(sfWebRequest $request){
        
	}
		  
}